package com.yn.courseselectionsystem.service.impl;

import com.yn.courseselectionsystem.dao.CourseDao;
import com.yn.courseselectionsystem.dao.CourseRecordDao;
import com.yn.courseselectionsystem.pojo.Course;
import com.yn.courseselectionsystem.pojo.CourseRecord;
import com.yn.courseselectionsystem.pojo.User;
import com.yn.courseselectionsystem.service.CourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: TODO
 * @author: Tokyo_God
 * @date: 2021/1/3 10:56
 * Role of:
 * @version: v1.0
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    CourseDao courseDao;
    @Resource
    CourseRecordDao courseRecordDao;

    @Override
    public List<Course> selCourse(Integer elect, Course course, User user) {
        return courseDao.selCourse(elect, course, user);
    }

    @Override
    public Course selCourseId(Integer id) {
        return courseDao.selCourseId(id);
    }

    //选课   （可以考虑在选课的时候加学分）    已在另一个项目中写了 这个方法没必要存在了
    @Override
    public int takeTheCourse(CourseRecord courseRecord) {
        int num=1;
        try {
            //课程数量加1   在修改课程的数量
            courseRecordDao.addCourseRecord(courseRecord);//添加
            courseDao.upCourseAmount(courseRecord.getCourseId());//课程数量加1
        }catch (Exception e){
            num=0;
        }
        return num;
    }

    //退课
    @Transactional
    @Override
    public int dropTheCourse(Course course, User user) {
        int num=0;
        int num1=courseDao.upCourseAddAmount(course.getId());//添加课程数量
        //修改状态为退课状态
        int num2=courseRecordDao.upCRIfTheSelected(user.getId(),course.getId());//
        num=(num1>0&&num2>0)?1:0;
        return num;
    }
}
